Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import { NextResponse } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { updateFlag, deleteFlag } from '@/lib/feature-flags' /** * PATCH /api/admin/feature-flags/[key] * * Update a feature flag (admin only). */ export const PATCH = withAuth( async (request, { params }) => { const { key } = (await params) as { key: string } try { const body = await request.json() const update: { enabled?: boolean config?: string | null description?: string | null allowedRoles?: string | null } = {} if ('enabled' in body) { update.enabled = Boolean(body.enabled) } if ('description' in body) { update.description = body.description ?? null } if ('config' in body) { if (body.config === null) { update.config = null } else if (typeof body.config === 'string') { // Validate it's valid JSON try { JSON.parse(body.config) update.config = body.config } catch { return NextResponse.json({ error: 'Config must be valid JSON' }, { status: 400 }) } } else { update.config = JSON.stringify(body.config) } } if ('allowedRoles' in body) { if (body.allowedRoles === null) { update.allowedRoles = null } else if (Array.isArray(body.allowedRoles)) { if (!body.allowedRoles.every((r: unknown) => typeof r === 'string')) { return NextResponse.json( { error: 'allowedRoles must be an array of strings' }, { status: 400 } ) } update.allowedRoles = body.allowedRoles.length > 0 ? JSON.stringify(body.allowedRoles) : null } else { return NextResponse.json( { error: 'allowedRoles must be an array of strings or null' }, { status: 400 } ) } } const updated = await updateFlag(key, update) if (!updated) { return NextResponse.json({ error: 'Flag not found' }, { status: 404 }) } return NextResponse.json({ success: true, key }) } catch (error) { console.error('[feature-flags] Update failed:', error) return NextResponse.json({ error: 'Failed to update feature flag' }, { status: 500 }) } }, { role: 'admin' } ) /** * DELETE /api/admin/feature-flags/[key] * * Delete a feature flag (admin only). */ export const DELETE = withAuth( async (_request, { params }) => { const { key } = (await params) as { key: string } try { const deleted = await deleteFlag(key) if (!deleted) { return NextResponse.json({ error: 'Flag not found' }, { status: 404 }) } return NextResponse.json({ success: true, key }) } catch (error) { console.error('[feature-flags] Delete failed:', error) return NextResponse.json({ error: 'Failed to delete feature flag' }, { status: 500 }) } }, { role: 'admin' } ) |